package com.ljc.service;

import com.ljc.dao.AdministratorDao;
import com.ljc.entity.Administrator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class AdministratorServiceImpl  implements AdministratorService{
    @Autowired
    private AdministratorDao administratorDao;

    @Override
    public void register(Administrator administrator) {
        Administrator administratorDB = administratorDao.findByName(administrator.getName());
        if(administratorDB!=null){
            throw new RuntimeException("该用户名已存在");
        }else{
            administrator.setPassword(DigestUtils.md5DigestAsHex(administrator.getPassword().getBytes()));
            administratorDao.addAdministrator(administrator);
        }
    }

    @Override
    public Administrator login(Administrator administrator) {
        Administrator administratorDB = administratorDao.findByName(administrator.getName());
        administrator.setPassword(DigestUtils.md5DigestAsHex(administrator.getPassword().getBytes()));

        if(administratorDB!=null){
            if(!administrator.getPassword().equals(administratorDB.getPassword())){
                throw new RuntimeException("密码错误");
            }else {
                return administratorDB;
            }
        }else{
            throw new RuntimeException("该用户名不存在");
        }
    }

    @Override
    public Administrator getInfo(int id) {
        return administratorDao.selectById(id);
    }

    @Override
    public void update(Administrator administrator) {
        administratorDao.update(administrator);
    }

    @Override
    public void modifyPassword(int id, String password) {
        administratorDao.modifyPassword(id,password);
    }

}
